//
//  BaiduMapViewController.m
//  DuckAndDoc
//
//  Created by 蓝水软件 on 2017/6/2.
//  Copyright © 2017年 苏州蓝水有限公司. All rights reserved.
//

#import "BaiduMapViewController.h"
#import "MapTool.h"
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>

@interface BaiduMapViewController ()<BMKPoiSearchDelegate,BMKLocationServiceDelegate,BMKMapViewDelegate>
@property (nonatomic,strong) BMKMapView *mapView;
@property (nonatomic,strong) BMKPoiSearch *mapSearch;   //搜索服务
@property (nonatomic,strong) BMKLocationService *locationService;  //定位服务
@property (nonatomic,strong) BMKMapStatus *mapStatus;   //地图状态
@property (nonatomic,strong) BMKPointAnnotation *selfAnnotation;   //当前位置
@property (nonatomic,strong) BMKPointAnnotation *meetAnnotation;//培训位置
@property (nonatomic,assign) double meetLocationRadius;
@end

@implementation BaiduMapViewController

-(void)viewWillAppear:(BOOL)animated
{
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
}

-(void)viewWillDisappear:(BOOL)animated
{
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时，置nil
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}

- (void)setUI{
    [self.locationService startUserLocationService];
    self.view = self.mapView;
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"签到" style:UIBarButtonItemStylePlain target:self action:@selector(sign)];
    self.navigationItem.rightBarButtonItem = rightBarItem;
    self.meetLocationRadius = 1000;
}

- (void)sign{
    BOOL inMeetRange = [MapTool isAccessRangeWithRadius:self.meetLocationRadius myLocation:self.selfAnnotation.coordinate meetLocation:self.meetAnnotation.coordinate];
    if (inMeetRange) {
        [QXToast showTopWithText:@"签到成功"];
    }else{
        [QXToast showTopWithText:@"不在培训范围内，不能签到"];
    }
}

#pragma mark -- BMKMapViewDelegate
//选取位置
- (void)mapview:(BMKMapView *)mapView onLongClick:(CLLocationCoordinate2D)coordinate{
    [self.selectAnnotation setCoordinate:coordinate];
    [self.mapView addAnnotation:self.selectAnnotation];
    //添加范围形状
    BMKCircle *circle = [BMKCircle circleWithCenterCoordinate:coordinate radius:self.meetLocationRadius];
    [self.mapView removeOverlays:self.mapView.overlays];
    [self.mapView addOverlay:circle];
}

- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay{
    if ([overlay isKindOfClass:[BMKCircle class]]){
        BMKCircleView* circleView = [[BMKCircleView alloc] initWithOverlay:overlay] ;
        circleView.fillColor = [[UIColor flatBlueColor] colorWithAlphaComponent:0.1];
        circleView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];
        circleView.lineWidth = 0.5;
        return circleView;
    }
    return nil;
}

// Override
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if (annotation == self.selectAnnotation) {
        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"selectAnnotation"];
        newAnnotationView.pinColor = BMKPinAnnotationColorGreen;
        newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
        return newAnnotationView;
    }
    return nil;
}

#pragma mark -- BMKPoiSearchDelegate
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResult errorCode:(BMKSearchErrorCode)errorCode{
    if (errorCode) {
        NSLog(@"检索失败：%d",errorCode);
    }else{
        for (BMKPoiInfo *info  in poiResult.poiInfoList) {
            BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
            annotation.coordinate = info.pt;
            annotation.title = info.address;
            annotation.subtitle = info.name;
            [self.mapView addAnnotation:annotation];
        }
    }
}

//位置更新后调用
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
    if (!self.selfAnnotation.coordinate.longitude) {
        NSLog(@"更新地图中心点");
        [self.mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
    }
    [self.selfAnnotation setCoordinate:userLocation.location.coordinate];
    [self.mapView addAnnotation:self.selfAnnotation];
}

#pragma mark -- lazy load
- (BMKMapView *)mapView{
    if (_mapView == nil) {
        _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
        [_mapView setMapStatus:self.mapStatus withAnimation:YES];
        _mapView.mapType = BMKMapTypeStandard;
        _mapView.delegate = self;
    }
    return _mapView;
}

- (BMKPoiSearch *)mapSearch{
    if (_mapSearch == nil) {
        _mapSearch = [[BMKPoiSearch alloc] init];
        _mapSearch.delegate = self;
    }
    return _mapSearch;
}

- (BMKLocationService *)locationService{
    if (_locationService == nil) {
        _locationService = [[BMKLocationService alloc] init];
        _locationService.delegate = self;
        _mapView.showsUserLocation = YES;
    }
    return _locationService;
}

- (BMKMapStatus *)mapStatus{
    if (_mapStatus == nil) {
        _mapStatus = [[BMKMapStatus alloc] init];
        _mapStatus.fLevel = 16;
    }
    return _mapStatus;
}

- (BMKPointAnnotation *)selectAnnotation{
    if (_meetAnnotation == nil) {
        _meetAnnotation = [[BMKPointAnnotation alloc] init];
        _meetAnnotation.title = @"培训地点";
    }
    return _meetAnnotation;
}

- (BMKPointAnnotation *)selfAnnotation{
    if (_selfAnnotation == nil) {
        _selfAnnotation = [[BMKPointAnnotation alloc] init];
        _selfAnnotation.title = @"我的位置";
    }
    return _selfAnnotation;
}

@end
